package com.ccp.dev.qbdms.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.DateUtil;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.extend.service.ExtendSysOrgService;
import com.ccp.dev.form.service.impl.ScriptImpl;
import com.ccp.dev.qbdms.consts.ZhiLiangConst;
import com.ccp.dev.qbdms.expimp.util.AbstractExpUtil;
import com.ccp.dev.qbdms.expimp.util.AuxiliaryUtil;
import com.ccp.dev.qbdms.model.WQbdDpaInfo;
import com.ccp.dev.qbdms.model.vo.TopFiveProblemVo;
import com.ccp.dev.qbdms.service.TopFiveProblemService;
import com.ccp.dev.system.model.Dictionary;
import com.ccp.dev.system.model.SysOrg;
import com.ccp.dev.system.service.SysOrgService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by zzy on 2020/7/21.
 *
 * @Author zzy
 * @Date 2020/7/21
 * @describe
 */
@Controller
@Action(customModel = "前五位质量问题统计")
@RequestMapping("/qbdms/topfiveproblem")
public class TopFiveProblemController extends SysBaseController {
    @Resource
    private TopFiveProblemService topFiveProblemService;
    @Resource
    public SysOrgService sysOrgService;
    @Resource
    public ScriptImpl scriptimpl;
    @Resource
    public ExtendSysOrgService extendSysOrgService;

    /**
     * 返回页面
     * @return
     */
    @GetMapping("/index")
    @Action(description = "跳转到前五位质量问题统计页面", exectype = "页面跳转日志")
    public String getView(Model model){
        String orgid = null;
        try {
            orgid = scriptimpl.getCurrentOrgId();
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<SysOrg> orglist = extendSysOrgService.querySubSysOrgBySupOrgId(orgid);
        orglist.add(scriptimpl.getCurrentOrg());
        List<Dictionary> dicList = new ArrayList<>();
        for (SysOrg sysOrg : orglist) {
            Dictionary dictionary = new Dictionary();
            dictionary.setItemName(sysOrg.getOrgName());
            dictionary.setItemValue(sysOrg.getCode());
            dicList.add(dictionary);
        }
        model.addAttribute("dw", JSON.toJSONString(dicList));
        return "/topfiveproblem/index";
    }

    /**
     * 获取统计数据
     * @return
     */
    @RequestMapping("/getdata")
    @ResponseBody
    @Action(description = "获取前五位质量问题统计数据信息", detail = "获取前五位质量问题统计数据信息<#if success>成功<#else>失败</#if>")
    public Object getData(HttpServletRequest request){
        String dw = RequestUtil.getString(request, "dw");
        String starttime = RequestUtil.getString(request, "starttime");
        String endtime = RequestUtil.getString(request, "endtime");
        String productname = RequestUtil.getString(request, "productname","");

        if (StringUtil.isEmpty(dw) || StringUtil.isEmpty(starttime) || StringUtil.isEmpty(endtime)) {
            SysAuditThreadLocalHolder.putParamerter("success",false);
            return fail("参数错误!");
        }
        Date start = DateUtil.parseDate(starttime);
        Date end = DateUtil.parseDate(endtime);
        SysAuditThreadLocalHolder.putParamerter("success",true);
        return success(topFiveProblemService.getData(start, end, dw, productname));
    }

    /**
     * 导出数据
     */
    @RequestMapping("/exportData")
    @Action(description="导出前五位质量问题统计数据信息",detail="导出前五位质量问题统计数据信息<#if success>成功<#else></#if>")
    public void export(HttpServletRequest request,HttpServletResponse response) throws IOException, IllegalAccessException, NoSuchFieldException, InstantiationException {
        String dw = RequestUtil.getString(request, "dw");
        String starttime = RequestUtil.getString(request, "starttime");
        String endtime = RequestUtil.getString(request, "endtime");
        String productname = RequestUtil.getString(request, "productname","");
        Date start = DateUtil.parseDate(starttime);
        Date end = DateUtil.parseDate(endtime);

        SysAuditThreadLocalHolder.putParamerter("success",false);
        String[] id = RequestUtil.getStringAryByStr(request,"id");
        QueryFilter queryFilter = new QueryFilter(request,false);
        JSONArray dataArray = ZhiLiangConst.exportAddConditions(queryFilter,id);

        List<TopFiveProblemVo> datalist = topFiveProblemService.exportExcel(start, end, dw, productname);

        if(datalist.isEmpty()){
            datalist.add(new TopFiveProblemVo());
        }
        new AbstractExpUtil() {
            @Override
            public String customAnalysis(String fieldName, String fieldValue) {
                return null;
            }
        }.executeExport(request,response,datalist,dataArray,"前五位质量问题统计", AbstractExpUtil.ExpStyle.MANY_SHEET);
        SysAuditThreadLocalHolder.putParamerter("success",true);

    }

    /**
     * 功能描述: 跳转到导出选择字段页面
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/export")
    @Action(description = "跳转到前五位统计导出选择字段页面", exectype = "页面跳转日志")
    public String export(HttpServletRequest request, ModelMap modelMap) throws Exception {
        com.alibaba.fastjson.JSONObject jsonObject = AuxiliaryUtil
                .resolverModel(TopFiveProblemVo.class);
        modelMap.put("tableInfo", jsonObject.toString());
        return "expImp/add";
    }

}
